package br.com.ravi.dao;

import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.com.ravi.utils.Cryptografia;
import br.com.ravi.utils.MySqlConection;
import br.com.ravi.vo.UserVO;

public class LoginDAO {

	public UserVO login(String login, String senha) throws SQLException, NoSuchAlgorithmException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet resultSet = null;
		UserVO userVO = null;
		
		try {
			// conecta banco
			con = MySqlConection.getConexaoMySQL();
			String sql = "select dcr_nome from cadusuario where dcr_usuario = ? and senha = ?";
			ps = con.prepareStatement(sql);
			ps.setString(1, Cryptografia.Criptografa(login));
			ps.setString(2, Cryptografia.Criptografa(senha));
	
			resultSet = ps.executeQuery();
	
			if (resultSet != null) {
				if (resultSet.next()) {
					String dcrNome = resultSet.getString("dcr_nome");
					userVO = new UserVO();
					userVO.setDcrNome(dcrNome);
				}
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			if (resultSet != null) {
				resultSet.close();
			}
			if (ps !=  null) {
				ps.close();
			}
		}
		
		return userVO;
	}
}
